﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataTypeAttribute.cs" company="CodePlex">
//   The MIT License
//   Copyright (c) 2013 Julien Blin
//   
//   Permission is hereby granted, free of charge, to any person obtaining a copy
//   of this software and associated documentation files (the "Software"), to deal
//   in the Software without restriction, including without limitation the rights
//   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//   copies of the Software, and to permit persons to whom the Software is
//   furnished to do so, subject to the following conditions:
//   
//   The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//   
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//   THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace UCodeFirst.DataTypes
{
    using System;

    using umbraco.interfaces;

    using Umbraco.Core.Models;

    /// <summary>
    /// Allows the definition of data types attributes.
    /// The Name is used as the key for modifications.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    public sealed class DataTypeAttribute : NameAttributeBase
    {
        /// <summary>
        /// The render control data type.
        /// </summary>
        private readonly Type renderControlDataType;

        /// <summary>
        /// The GUID for a render control.
        /// </summary>
        private readonly string renderControlGuid;

        /// <summary>
        /// The database type.
        /// </summary>
        private readonly DataTypeDatabaseType databaseType;

        /// <summary>
        /// Initializes a new instance of the <see cref="DataTypeAttribute"/> class.
        /// </summary>
        /// <param name="renderControlGuid">
        /// The GUID for a render control.
        /// </param>
        /// <param name="databaseType">
        /// The database type.
        /// </param>
        public DataTypeAttribute(string renderControlGuid, DataTypeDatabaseType databaseType)
        {
            this.renderControlGuid = renderControlGuid;
            this.databaseType = databaseType;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataTypeAttribute"/> class.
        /// </summary>
        /// <param name="renderControlDataType">
        /// The render control data type. This type must be a <see cref="IDataType"/>
        /// </param>
        /// <param name="databaseType">
        /// The database type.
        /// </param>
        public DataTypeAttribute(Type renderControlDataType, DataTypeDatabaseType databaseType)
        {
            this.renderControlDataType = renderControlDataType;
            this.databaseType = databaseType;
        }

        /// <summary>
        /// Gets the database type.
        /// </summary>
        public DataTypeDatabaseType DatabaseType
        {
            get
            {
                return this.databaseType;
            }
        }

        /// <summary>
        /// Gets the render control GUID, provided by either a GUID or a type.
        /// The GUID has precedence over the type.
        /// </summary>
        /// <returns>
        /// The render control <see cref="Guid"/>.
        /// </returns>
        internal Guid GetRenderControlGuid()
        {
            if (!string.IsNullOrEmpty(this.renderControlGuid))
            {
                return Guid.Parse(this.renderControlGuid);
            }

            return this.renderControlDataType.CreateInstance<IDataType>().Id;
        }
    }
}
